<template>
  <div class="app-container">
    <itemInfo :show="show"  :labelGroupId="labelGroupId" :labelId="labelId" @updateList="getItemList(labelGroupId)" @cancelDialog="cancelDialog"></itemInfo>
    <el-button type="primary" icon="el-icon-plus" @click="addLabel">添加标签项</el-button>
    <span class="select">标签组:</span>
    <el-select v-model="labelGroupId" placeholder="请选择标签组">
      <el-option
        v-for="item in options"
        :key="item.labelGroupId"
        :label="item.labelGroupName"
        :value="item.labelGroupId">
      </el-option>
    </el-select>
    <el-table :data="list" v-loading.body="listLoading" element-loading-text="Loading" border fit highlight-current-row max-height="550" class="groupTable">
      <el-table-column label="标签项名称" align="center">
        <template slot-scope="scope">
          {{scope.row.labelName}}
        </template>
      </el-table-column>
      <el-table-column label="标签项颜色" align="center">
        <template slot-scope="scope">
          <div class="colorBox" :style="{background:scope.row.color}"></div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="操作" width="350" >
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="editLabel(scope)">编辑</el-button>
          <el-button type="danger" size="mini" @click="deleteLabel(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import itemInfo from '../components/itemInfo'
import { labelList, labelDelete, labelGroupList } from 'api/label'
export default {
  components: {
    itemInfo
  },
  data() {
    return {
      list: null,
      listLoading: true,
      show: false,
      labelId: '',
      options: null,
      labelGroupId: this.$route.query.labelGroupId
    }
  },
  watch: {
    labelGroupId(val, oldval) {
      if (val !== '') {
        this.getItemList(val)
      }
    }
  },
  created() {
    this.getLabelGroupList()
    this.getItemList(this.labelGroupId)
  },
  methods: {
    async getLabelGroupList() {
      const parm = {
        curPage: 0,
        pageSize: 10000,
        labelGroupName: ''
      }
      try {
        let back = await labelGroupList(parm)
        back = back.data
        if (back.resultCode === '200') {
          this.options = back.result
          if (!this.$route.query.labelGroupId) {
            this.labelGroupId = this.options[0].labelGroupId
          }
        }
      } catch (err) {
        this.$alert('请求失败', '提示')
      }
    },
    cancelDialog() {
      this.labelId = ''
      this.show = false
    },
    addLabel() {
      this.show = true
    },
    async getItemList(val) {
      try {
        let back = await labelList(val)
        back = back.data
        if (back.resultCode === '200') {
          this.list = back.result
          this.listLoading = false
        }
      } catch (err) {
        this.$alert('请求失败', '提示')
      }
    },
    async deleteLabel(scope) {
      try {
        let back = await labelDelete(scope.row.labelId)
        back = back.data
        if (back.resultCode === '200') {
          this.getItemList(this.labelGroupId)
        } else {
          this.$alert(back.msg)
        }
      } catch (err) {
        this.$alert('请求出错', '提示')
      }
    },
    editLabel(scope) {
      this.labelId = scope.row.labelId
      this.show = true
    }
  }
}
</script>
<style lang="scss" scoped>
  .groupTable{
    margin-top:20px;
  }
  .inlineBlock{
    display: inline-block;
    margin:0 20px;
  }
  .select{
    display: inline-block;
    margin-left:50px;
    margin-right:10px;
  }
  .colorBox{
    width: 20px;
    height: 20px;
    background: red;
    margin:0 auto;
  }
</style>
